package com.ck.service.impl;

import com.ck.entity.Commodity;
import com.ck.entity.Worker;
import com.ck.mapper.CommodityMapper;
import com.ck.mapper.WorkerMapper;
import com.ck.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private CommodityMapper mapper;
    @Autowired
    private WorkerMapper workerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveCommodity(Commodity commodity) {
        mapper.insert(commodity);
    }

    @Override
    //@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public int saveWork(Worker worker) throws InterruptedException {
        Worker vWorker = workerMapper.selectById(worker.getId());
        if(vWorker == null) {
            synchronized (CommodityServiceImpl.class) {
                vWorker = workerMapper.selectById(worker.getId());
                if (vWorker == null) {
                    workerMapper.insert(worker);
                    vWorker = worker;
                }
            }
        }
        return vWorker.getId();
    }
}
